package com.beteng.su.betengsu.net.webservice;


import android.util.Log;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.HashMap;

/**
 * @author linchenqiao
 * @date 2015/11/28.
 * @Description
 */
public class LastestWebServiceUtils {

    private static final String TAG = "LastestWebServiceUtils";
    public static final int TIMEOUT = 60 * 1000;


    /**
     * @param url        WebService服务器地址
     * @param methodName WebService的调用方法名
     * @param strProps   WebService的参数 所有 调用webservice通过该方法
     */
    @SuppressWarnings("finally")
    public static SoapObject callWebService(String url, String methodName,
                                            HashMap<String, Object> strProps, String nameSpace, String Data) {
        SoapObject resultSoapObject = null;
        // 创建HttpTransportSE对象，传递WebService服务器地址
        HttpTransportSE httpTransportSE = null;
        try {
            httpTransportSE = new HttpTransportSE(url, TIMEOUT);
        } catch (Exception e1) {
            System.out.println(e1.getMessage());
            e1.printStackTrace();
            return resultSoapObject;
        }
        // 创建SoapObject对象
        SoapObject soapObject = new SoapObject(nameSpace, methodName);
        //
        //// SoapObject添加参数
        if (strProps!=null&&strProps.size()!= 0 ) {
            soapObject.addProperty("key",strProps.get("key").toString());

            if (strProps.get("ticket")!=null) {
                soapObject.addProperty("ticket",strProps.get("ticket").toString());
            }else {
                soapObject.addProperty("ticket",null);
            }
            soapObject.addProperty("data",strProps.get("data").toString());
        }

        Log.e(TAG,"Request:: Method: "+methodName+"data: "+strProps.get("data").toString());

        // soapheader在这里
        //Element[] header = new Element[1];
        //header[0] = new Element().createElement(nameSpace, "MySoapHeader");
        //Element json = new Element().createElement(nameSpace, "Json");
        //json.addChild(Node.TEXT, Data);
        //header[0].addChild(Node.ELEMENT, json);
        //Element version = new Element().createElement(nameSpace, "Version");
        //version.addChild(Node.TEXT, "1.0");
        //header[0].addChild(Node.ELEMENT, version);
        //Element actionType = new Element().createElement(nameSpace, "ActionType");
        //actionType.addChild(Node.TEXT, "1001");
        //header[0].addChild(Node.ELEMENT, actionType);
        // 实例化SoapSerializationEnvelope，传入WebService的SOAP协议的版本号
        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        // 设置是否调用的是.Net开发的WebService
        soapEnvelope.setOutputSoapObject(soapObject);
        soapEnvelope.dotNet = true;
        //soapEnvelope.headerOut = header;
        httpTransportSE.debug = true;

        try {
            httpTransportSE.call(nameSpace + methodName,soapEnvelope);
            if (soapEnvelope.getResponse() != null) {
                // 获取服务器响应返回的SoapObject
                resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return resultSoapObject;
        }
    }
}
